package com.Offer;

/*
    面试题58：翻转字符串
    输入一个英文句子，翻转句子中单词的顺序，但单词内字符的顺序不变
    例如"I am a student.",则输出"student. a am I"
 */
public class demo58 {
    public static void main(String[] args) {
        String s = "I am a student.";
        System.out.println(Reverse(s));
        String b = "abcdefg";
        System.out.println(leftReverse(b,0));
    }

    public static String Reverse(String str){
        if (str == null || str.length() == 0)
            return str;
        String[] strs = str.split(" ");
        StringBuffer buffer = new StringBuffer();
        for(int i = strs.length-1;i>=0;i--){
            buffer.append(strs[i]);
            if(i > 0){
                buffer.append(" ");
            }
        }
        return buffer.toString();
    }

    //举一反三：左旋转字符串，把字符串前面的若干个字符转移到字符串的尾部。例如'abcdefg'和数字2,该函数返回左旋转2位得到的结果'cdefgab'
    public static String leftReverse(String str,int k){
        if (str == null || str.length() < k || k <= 0)
            return str;
        String s1 = str.substring(0,k);
        String s2 = str.substring(k,str.length());
        return s2+s1;
    }
}
